from .models import User


def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'id': user.id,
        'name': user.username,
        'stated': user.stated
    }


def get_user_by_account(account):
    """
    根据帐号获取user对象s
    :param account: 账号，可以是用户名username，也可以是手机号mobile, 或者其他的数据
    :return: User对象 或者 None
    """
    try:
        user = User.objects.filter(username=account).first()
    except User.DoesNotExist:
        return None
    else:
        return user


def save(flag, information):
    dic = {}
    if flag == 1:
        dic = {'username': information}
    if flag == 2:
        dic = {'first_name': information.split(',')[0], 'last_name': information.split(',')[1]}
    if flag == 3:
        dic = {'email': information}
    return dic
